/*
 * Created on May 22, 2004
 */
package com.scalar.chiptrack.operations.process.actions;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.users.User;

/**
 * This is action for Migration of Finished Product Data from 
 * Chiptrac to Epicore
 * 
 * @author Ravikanth
 * @version 1.0
*/

public class SyncProductsAction extends ChipTrackAction {

	public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
	{

		HashMap errors  = ( HashMap ) request.getAttribute( SessionKeys.ERROR_MESSAGES );
		HashMap info    = ( HashMap ) request.getAttribute( SessionKeys.INFO_MESSAGES );
		
		if( errors == null )
		{
			errors = new HashMap();
		}

		if( info == null )
		{
			info = new HashMap();
		}
		ActionForward actionForward = mapping.findForward( "success" );
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute (SessionKeys.USER_KEY);
		if ( user == null )
		{
			session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, "/productsetup.do?" + request.getQueryString());
			return ( mapping.findForward( "login" ) );
		}

		try
		{
			ProductDao.syncFinishedGoodsToEpicore( user.getFirstName() );
			info.put( "Products Succesfully transfered to Epicore ", null );
		}
		catch( Exception e )
		{
			e.printStackTrace();
			errors.put( ERROR_INTERNAL_SERVER_ERROR, "Could not migrate the Chiptrac products to Epicore" );
		}
		
		request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
		request.setAttribute( SessionKeys.INFO_MESSAGES, info );
		
		actionForward = mapping.findForward( "success" );
		return actionForward;
	}	
}